package org.whatever.aha.zjut.platform.service.project;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.whatever.aha.zjut.base.constant.ErrorCode;
import org.whatever.aha.zjut.base.exception.AppException;
import org.whatever.aha.zjut.platform.dto.project.ProjectTeacherDto;
import org.whatever.aha.zjut.platform.entity.project.ProjectMember;
import org.whatever.aha.zjut.platform.entity.project.ProjectReturnLog;
import org.whatever.aha.zjut.platform.entity.project.ProjectTeacher;
import org.whatever.aha.zjut.platform.mapper.project.ProjectTeacherMapper;
import org.whatever.aha.zjut.platform.util.BeanUtil;
import org.whatever.aha.zjut.platform.vo.project.ProjectTeacherVo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Vc，Elder Master
 * @version 2.0
 * @Desc
 * @date 2021/11/08 0:29
 */
@RequiredArgsConstructor
@Service
public class ProjectTeacherService {

    final ProjectTeacherMapper projectTeacherMapper;

    /**
     * 根据项目Id获取教师信息
     *
     * @param projectId
     * @return
     */
    public List<ProjectTeacherVo> getProjectTeachers(int projectId){
        List<ProjectTeacher> projectTeachers = projectTeacherMapper.selectList(new QueryWrapper<ProjectTeacher>().eq("project_id",projectId));
        return BeanUtil.copyListProperties(projectTeachers, ProjectTeacherVo::new);
    }

    /**
     * 增加项目老师信息
     *
     * @param projectTeacherDto
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation= Propagation.REQUIRED)
    public Integer addProjectTeacherById(ProjectTeacherDto projectTeacherDto){
        ProjectTeacher projectTeacher = new ProjectTeacher();
        BeanUtils.copyProperties(projectTeacherDto, projectTeacher);
        return projectTeacherMapper.insert(projectTeacher);
    }

    /**
     * 修改项目老师信息
     *
     * @param projectTeacherDto
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation= Propagation.REQUIRED)
    public int updateProjectTeacher(ProjectTeacherDto projectTeacherDto){
        if(projectTeacherMapper.selectCount(new QueryWrapper<ProjectTeacher>().eq("project_id", projectTeacherDto.getProjectId()).eq("teacher_project_rank", projectTeacherDto.getTeacherProjectRank()))<1){
            throw new AppException(ErrorCode.PROJECT_TEACHER_LACK);
        }
        ProjectTeacher projectTeacher = new ProjectTeacher();
        BeanUtils.copyProperties(projectTeacherDto, projectTeacher);
        return projectTeacherMapper.update(projectTeacher, new QueryWrapper<ProjectTeacher>().eq("project_id", projectTeacherDto.getProjectId()).eq("teacher_project_rank", projectTeacherDto.getTeacherProjectRank()));
    }

    /**
     * 删除项目老师信息
     *
     * @param projectId
     * @param teacherProjectRank
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation= Propagation.REQUIRED)
    public int deleteProjectTeacher(int projectId, Integer teacherProjectRank){
        if(teacherProjectRank != null && projectTeacherMapper.selectCount(new QueryWrapper<ProjectTeacher>().eq("project_id",projectId).eq("teacher_project_rank", teacherProjectRank))<1){
            throw new AppException(ErrorCode.PROJECT_TEACHER_LACK);
        }
        return projectTeacherMapper.delete(new QueryWrapper<ProjectTeacher>().eq("project_id", projectId).eq(teacherProjectRank!=null, "teacher_project_rank", teacherProjectRank));
    }
}